package com.sbck.voiceclient2;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.sbck.voiceclient2.myclass.MyInputMethodService;

public class MainActivity extends AppCompatActivity {

    public SocketUtil cSocketUtil;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        MyInputMethodService myInputMethodService = new MyInputMethodService();

        EditText editText = findViewById(R.id.tbx);
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // 检查文本变化
                if (s.toString().endsWith("。") || s.toString().endsWith(".") || s.toString().endsWith("！") || s.toString().endsWith("？"))  {
                    // 句子结束
                    Log.d("SpeechInput", "Sentence completed: " + s.toString());
                    cSocketUtil.SendSocketData(s.toString());
                    editText.setText(0);
//                    editText.setFocusable(true);
//                    editText.setFocusableInTouchMode(true);
//                    editText.requestFocus();
                }
            }

            @Override
            public void afterTextChanged(Editable s) {}
        });

        cSocketUtil = new SocketUtil();
        cSocketUtil.startClient();

//        InputMethodManager inputMethodManager = (InputMethodManager) editText.getContext().getSystemService(INPUT_METHOD_SERVICE);
//        inputMethodManager.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);

    }
}